package com.zhuiYun.project.api.seckill.mapper;

import com.zhuiYun.project.api.seckill.entity.SeckillDto;
import com.zhuiYun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @ClassName SeckillServiceImpl
 * @Description TODO
 * @Author lx
 * @Date 2023/4/3 15:00
 **/
@Mapper
public interface SeckillDtoMapper extends BaseMapper<SeckillDto> {
    /**
     * @Author lx
     * @Description 首页展示5个限时抢购的商品
     * @Date 2023/4/3 19:32
     * @Return java.util.List<com.zhuiYun.project.api.seckill.entity.SeckillEntity>
     */
    @Select("select c.id,c.commodity_name,s.id,s.comm_id,c.image_id,c.del_flag,c.back,s.seckill_price,s.seckill_stock,s.seckill_start_date,s.seckill_end_date,s.del_flag\n" +
            "            from commodity c,seckill s \n" +
            "            where s.comm_id = c.id and s.del_flag = 0 and c.del_flag = 0 and s.seckill_end_date>now()\n" +
            "\t\t\t\t\t\tlimit 0,4")
    List<SeckillDto> selectGetSeckillAll(SeckillDto seckillDto);

    /**
     * @Author lx
     * @Description 显示抢购商品信息
     * @Date 2023/4/3 19:32
     * @Return java.util.List<com.zhuiYun.project.api.seckill.entity.SeckillEntity>
     */
    @Select("select c.id,c.commodity_name,c.del_flag,c.image_id,c.back,s.id,s.comm_id,s.seckill_price,s.seckill_stock,s.seckill_start_date,s.seckill_end_date,s.del_flag\n" +
            "from commodity c,seckill s\n" +
            "where s.del_flag = 0 and c.del_flag = 0 and s.comm_id = c.id and s.seckill_end_date>now() and s.seckill_start_date<now()")
    List<SeckillDto> selectGetSeckillList(SeckillDto seckillDto);


    @Select("select c.id,c.commodity_name,c.image_id,s.id,s.comm_id,s.seckill_price,s.seckill_stock,s.seckill_start_date,s.seckill_end_date,s.del_flag\n" +
            "from commodity c,seckill s\n" +
            "where s.del_flag = 0 and c.del_flag = 0 and s.comm_id = c.id and #{seckillStartDate} = s.seckill_start_date and #{seckillEndDate} =s.seckill_end_date")
    List<SeckillDto> selectGetSeckillListAll(SeckillDto seckillDto);

    /**
     * @Author lzk
     * @Description 限时抢购查询秒杀表中商品信息
     * @date 2023/4/5 9:54
     * Return java.util.List<com.zhuiYun.project.api.seckill.entity.SeckillDto>
     */
    @Select("select s.*,c.commodity_name,c.image_id,c.price from seckill s left join commodity c on s.comm_id = c.id where s.del_flag = 0 ")
//    <when test = 'commodity_name != &quot;&quot; and commodity_name != null'> " +
//            " and carName like '%${commodityName}%'</when>
    List<SeckillDto> queryFlashKillInformation(@Param("commodityName") String commodityName);
    /**
     * @Author lzk
     * @Description 根据id查询秒杀商品信息
     * @date 2023/4/6 13:52
     * Return com.zhuiYun.project.api.seckill.entity.SeckillDto
     * @param id
     */
    @Select("select s.*,c.commodity_name,c.image_id,c.price,c.id as cid,c.back,c.commodity_name,c.goods_quantity,ABS(c.goods_quantity - s.seckill_stock) AS sold " +
            "FROM seckill s LEFT JOIN commodity c ON s.comm_id = c.id WHERE s.del_flag = 0 and s.id = #{id} ")
    SeckillDto getSeckillListById(@Param("id") Integer id);

    /**
     * @Author lx
     * @Description 秒杀时查询库存
     * @Date 2023/4/6 15:07
     * @Return int
     * @param commId
     */
    @Select("select c.id,c.commodity_name,c.del_flag,c.back,c.image_id,s.id,s.comm_id,s.seckill_price,s.seckill_stock,s.seckill_start_date,s.seckill_end_date,s.del_flag\n" +
            "            from commodity c,seckill s\n" +
            "            where s.del_flag = 0 and c.del_flag = 0 and s.comm_id = c.id and s.seckill_end_date>now() and comm_id = #{comm_id}")
    SeckillDto selectByCommId(Integer commId);

    /** 
     * 
     * @Author lzk
     * @Description 根据cid查询秒杀和商品信息
     * @date 2023/4/9 19:20
     * Return java.util.List<com.zhuiYun.project.api.seckill.entity.SeckillDto>
     * @param cid
     */
    @Select("select s.*,c.back,c.commodity_name,c.price from seckill s left join commodity c on s.comm_id = c.id where s.comm_id = #{cid} ")
    SeckillDto getSeckillListByCid(@Param("cid") Integer cid);

    @Select("select s.*,c.*\n" +
            "from seckill s,commodity c\n" +
            "where c.id=s.comm_id and c.id=#{id}")
    List<SeckillDto> getSeckillMain(SeckillDto seckillDto);
}
